<?php

namespace App\Http\Validate\OpenShop\Organization;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageModel;
use App\Models\OpenShop\Organization\OpenShopRegionManageMappingModel;
use App\Models\OpenShop\Organization\OpenShopRegionManageModel;
use App\Models\OpenShop\Organization\OpenShopRegionManageUserMappingModel;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class WarZoneValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [
            "organizationManageIds" => "省区",
            "chargeUsers"           => "负责人",
            "attachChargeUsers"     => "附属负责人",
        ];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [
        ];
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function detailValidate(array $params)
    {
        return self::getInstance()->validate(
            ["id"],
            $params,
            [
                "id" => [
                    "required",
                    "int",
                    Rule::exists((new OpenShopRegionManageModel())->getTable(), "id")->where("status", 0)
                ]
            ]
        );
    }

    /**
     * 更新校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function updateValidate(array $params)
    {
        return self::getInstance()->validate(
            ["id", "organizationManageIds", "chargeUsers", "attachChargeUsers"],
            $params,
            [
                "id"                           => [
                    "required",
                    "int",
                    Rule::exists((new OpenShopRegionManageModel())->getTable(), "id")->where("status", 0)
                ],
                "organizationManageIds"        => [
                    "required",
                    "array",
                    "min:1"
                ],
                "organizationManageIds.*"      => [
                    "required",
                    "int",
                    Rule::exists((new OpenShopOrganizationManageModel())->getTable(), "id")
                        ->where("type", 2)
                        ->where("status", 0),
                    function ($attribute, $value, $fail) use ($params) {
                        $exists = OpenShopRegionManageMappingModel::query()
                                                                  ->with("organizationManageRel")
                                                                  ->where("manageId", $value)
                                                                  ->where("isDeleted", 0)
                                                                  ->where("regionId", "<>", $params["id"] ?? 0)
                                                                  ->first();
                        if ($exists) {
                            $fail("{$exists->organizationManageRel->name} 已存在其他战区 请核实后重试！");
                        }
                    },
//                    Rule::unique((new OpenShopRegionManageMappingModel())->getTable(), "manageId")
//                        ->where("isDeleted", 0)->ignore($params["id"] ?? 0, "regionId")
                ],
                "chargeUsers"                  => "required|array|size:1",
                "chargeUsers.*.userId"         => [
                    "required",
                    "int"
                ],
                "chargeUsers.*.username"       => [
                    "required",
                    "string",
                    "max:64"
                ],
                "attachChargeUsers"            => "array",
                "attachChargeUsers.*.userId"   => [
                    "required",
                    "int"
                ],
                "attachChargeUsers.*.username" => [
                    "required",
                    "string",
                    "max:64"
                ]
            ]
        );
    }
}
